#ifndef CLIONQT5WORK_MAINWINDOW_QTCASED2_H
#define CLIONQT5WORK_MAINWINDOW_QTCASED2_H
#include <QMainWindow>
#include <QLabel>
#include <QImage>
#include <QRect>
#include <QMouseEvent>
#include <opencv2/opencv.hpp>

class ImageCanvas : public QLabel {
Q_OBJECT
public:
    explicit ImageCanvas(QWidget* parent = nullptr);

    void setImage(const cv::Mat& mat);
    void clearRectangles();

    void setSelectionRect(const QRect& rect); // 左图红框
    void setResultRect(const QRect& rect);    // 右图蓝框

    QRect getSelectionRect() const;

signals:
    void selectionChanged(const QRect& rect);

protected:
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void paintEvent(QPaintEvent* event) override;

private:
    QImage m_image;           // 存储当前显示图像
    QRect m_selectionRect;    // 选区红框
    QRect m_resultRect;       // 右图蓝框
    bool m_selecting = false;
    QPoint m_startPoint;

public:
    double getScale() const { return m_scale; }

private:
    double m_scale = 1.0; // 缩放比例
    void updateScale();
};

class MainWindow : public QMainWindow {
Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

private slots:
    void loadImage1();
    void loadImage2();
    void onSelectionChanged(const QRect& rect);

private:
    void updateDisplay();
    void alignImages();

    ImageCanvas* m_leftCanvas;
    ImageCanvas* m_rightCanvas;
    QLabel* m_textLabel;

    cv::Mat m_img1, m_img2, m_img2_aligned;
    cv::Mat m_H;

    QRect m_currentSelection; // 当前左图选框（图像坐标）
};
#endif //CLIONQT5WORK_MAINWINDOW_H
